package main;


import java.io.File;
import javax.swing.JOptionPane;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Number;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Mainframe.java
 *
 * Created on Apr 13, 2011, 7:32:11 PM
 */

/**
 *
 * @author евгений
 */
public class Mainframe extends javax.swing.JFrame {

    /** Creates new form Mainframe */
    public Mainframe() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        WordPanel = new javax.swing.JPanel();
        Word = new javax.swing.JLabel();
        Translate = new javax.swing.JLabel();
        ButtonPanel = new javax.swing.JPanel();
        helpButton = new javax.swing.JButton();
        translateButton = new javax.swing.JButton();
        rightButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                formComponentHidden(evt);
            }
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        Word.setFont(new java.awt.Font("Times New Roman", 0, 24));
        Word.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Word.setText("Слово");

        Translate.setFont(new java.awt.Font("Times New Roman", 0, 24));
        Translate.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Translate.setText("Перевод");

        javax.swing.GroupLayout WordPanelLayout = new javax.swing.GroupLayout(WordPanel);
        WordPanel.setLayout(WordPanelLayout);
        WordPanelLayout.setHorizontalGroup(
            WordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, WordPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(WordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Word, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                    .addComponent(Translate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE))
                .addContainerGap())
        );
        WordPanelLayout.setVerticalGroup(
            WordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, WordPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Word, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Translate, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        ButtonPanel.setLayout(new java.awt.GridLayout(1, 0));

        helpButton.setText("Подсказать");
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });
        ButtonPanel.add(helpButton);

        translateButton.setText("Перевод");
        translateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                translateButtonActionPerformed(evt);
            }
        });
        ButtonPanel.add(translateButton);

        rightButton.setText("Правильно");
        rightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightButtonActionPerformed(evt);
            }
        });
        ButtonPanel.add(rightButton);

        nextButton.setText("Следующее");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        ButtonPanel.add(nextButton);

        jToolBar1.setRollover(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(WordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(WordPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        File inputWorkbook = new File(inputFile);
        if (allwords==null){
            allwords=new Learndata();
            try{
                Workbook w=Workbook.getWorkbook(inputWorkbook);
                Sheet sheet=w.getSheet(0);
                for (int i=0;i<sheet.getRows();i++){
                    allwords.add(sheet.getCell(0, i).getContents(), sheet.getCell(1, i).getContents(), new Integer(sheet.getCell(2, i).getContents()), new Integer(sheet.getCell(3, i).getContents()), new Integer(sheet.getCell(4, i).getContents()), new Integer(sheet.getCell(5, i).getContents()));
                }
                setLoaded(true);
                w.close();
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, "Ошибка импорта");
                e.printStackTrace();
            }
        }
        Word.setText(allwords.Show());
        Translate.setText("");
        nextButton.setEnabled(false);
        rightButton.setEnabled(false);

    }//GEN-LAST:event_formComponentShown

    private void translateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_translateButtonActionPerformed
        Translate.setText(allwords.Translate());
        nextButton.setEnabled(true);
        rightButton.setEnabled(true);

    }//GEN-LAST:event_translateButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        Word.setText(allwords.Show());
        Translate.setText("");
        nextButton.setEnabled(false);
        rightButton.setEnabled(false);
    }//GEN-LAST:event_nextButtonActionPerformed

    private void rightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightButtonActionPerformed
        allwords.setright();
        Word.setText(allwords.Show());
        Translate.setText("");
        nextButton.setEnabled(false);
        rightButton.setEnabled(false);
    }//GEN-LAST:event_rightButtonActionPerformed

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        int letter=Translate.getText().length()<allwords.Translate().length()?Translate.getText().length()+1:allwords.Translate().length();
        Translate.setText(allwords.Translate().substring(0, letter));
    }//GEN-LAST:event_helpButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
    }//GEN-LAST:event_formComponentHidden

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (!isLoaded())
            return;
        try{
            File file = new File(inputFile);
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            workbook.createSheet("Words", 0);
            WritableSheet excelSheet = workbook.getSheet(0);
            for(int i=0;i<allwords.size();i++){
                excelSheet.addCell(new Label(0,i,allwords.get(i).getWord()));
                excelSheet.addCell(new Label(1,i,allwords.get(i).getTranslate()));
                excelSheet.addCell(new Number(2,i,allwords.get(i).getShow()));
                excelSheet.addCell(new Number(3,i,allwords.get(i).getShow1()));
                excelSheet.addCell(new Number(4,i,allwords.get(i).getRight()));
                excelSheet.addCell(new Number(5,i,allwords.get(i).getRight1()));
            }
            workbook.write();
            workbook.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Mainframe().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JLabel Translate;
    private javax.swing.JLabel Word;
    private javax.swing.JPanel WordPanel;
    private javax.swing.JButton helpButton;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton rightButton;
    private javax.swing.JButton translateButton;
    // End of variables declaration//GEN-END:variables
    protected static final String inputFile = "words.xls";
    private Learndata allwords;
    private boolean loaded=false;

    public boolean isLoaded() {
        return loaded;
    }

    public void setLoaded(boolean loaded) {
        this.loaded = loaded;
    }

    public static String getInputFile() {
        return inputFile;
    }

}
